package org.bluweb.g4wicket.newsshow;

import java.util.HashMap;
import java.util.Map;

import org.bluweb.g4wicket.G4WicketComponent;

@SuppressWarnings("serial")
public class NewsShow extends G4WicketComponent {

	public static enum Format { LEADERBOARD, RECTANGLE };
	
	private Map<String,String> options = new HashMap<String, String>();
	
	public NewsShow(String id) {
		super(id);
		this.setOption( "topic", "s" );
	}

	public NewsShow setFormat( Format format ) {
		switch( format ) {
			case LEADERBOARD:
				this.setOption( "format", "728x90" );
				break;
			case RECTANGLE:
				this.setOption( "format", "300x250" );
		}
		return this;
	}
	
	private NewsShow setOption( String name, String value ) {
		this.options.put( name, value );
		return this;
	}	
	
	@Override
	protected void onBeforeRender() {
		super.onBeforeRender();
		
		Map<String,Object> parameters = new HashMap<String,Object>();
//		parameters.put( "url", this.rssUrl );
		
		StringBuilder builder = new StringBuilder();
		for( String name : options.keySet() ) {
			if( builder.length() > 0 ) {
				builder.append( ',' );
			}
			builder.append( name ).append( ":'" );
			builder.append( options.get( name ) ).append( "'" );
		}
		
		parameters.put( "options", builder.toString() );
		
		this.setJavaScriptTemplate( "NewsShow.js", true, parameters );
	}
}
